package com.azzcs.juc.i;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: wzg
 * @Date: 2021/6/18 上午11:35
 */
public class Main {
    private static volatile int i = 0;
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(5);
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int j = 0; j < 5; j++) {
            executor.submit(()->{
                for (int k = 0; k < 10000; k++) {
                    // i++
                    i = add(i);
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println(i);
        executor.shutdown();
    }

    private static int add(int i) {
        int temp = i;
        temp = temp + 1;
        return temp;
    }
}
